package com.service.totalauth.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.totalauth.dto.UserDto;
import com.service.totalauth.service.CompanyInfoService;
import com.service.totalauth.vo.AddQyCorpInfoDto;
import com.service.totalauth.vo.UpdateQyCorpInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * 配置信息表(CompanyInfo)表控制层
 *
 * @author makejava
 * @since 2021-12-03 09:38:47
 */
@RestController
@RequestMapping("companyInfo")
@Api(value = "配置飞书/钉钉/app应用信息接口", tags = "配置飞书/钉钉/app应用信息接口")
public class CompanyInfoController {
    /**
     * 服务对象
     */
    @Resource
    private CompanyInfoService companyInfoService;

    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增企业信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "addQyCorpInfo",produces="application/json")
    @ApiOperation(value = "新增企业信息")
    @ResponseBody
    public Result addQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                @RequestBody AddQyCorpInfoDto addQyCorpInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return companyInfoService.addQyCorpInfo(addQyCorpInfoDto,userDto);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 删除企业信息
     *
     * @param
     * @return
     */
    @GetMapping(value = "deleteQyCorpInfo",produces="application/json")
    @ApiOperation(value = "删除企业信息")
    @ResponseBody
    public Result deleteQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestParam @ApiParam("企业信息id") String companyInfoId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return companyInfoService.deleteQyCorpInfo(userId,companyInfoId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 更新企业信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "updateQyCorpInfo",produces="application/json")
    @ApiOperation(value = "更新企业信息")
    @ResponseBody
    public Result updateQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestBody UpdateQyCorpInfoDto updateQyCorpInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return companyInfoService.updateQyCorpInfo(updateQyCorpInfoDto);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 刷新url页面时拼接的参数
     *
     * @param
     * @return
     */
    @GetMapping(value = "freshQyCorpInfo",produces="application/json")
    @ApiOperation(value = "刷新url页面时拼接的参数")
    @ResponseBody
    public Result freshQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                  @RequestParam String companyInfoId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return companyInfoService.freshQyCorpInfo(companyInfoId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 获取企业信息列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "getQyCorpInfoList",produces="application/json")
    @ApiOperation(value = "获取企业信息列表")
    @ResponseBody
    public Result getUserInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                              @RequestParam(value = "type") @ApiParam("0--普通 1--飞书 2--钉钉 3--app 4--微信小程序 5--企业微信") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return companyInfoService.queryByUserId(userId,type);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 获取企业信息列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "getBindInfo",produces="application/json")
    @ApiOperation(value = "获取微信小程序已绑定的企业微信和未绑定的企业微信")
    @ResponseBody
    public Result getBindInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                              @RequestParam(value = "WechatId") @ApiParam("微信信息id") String WechatId,
                              @RequestParam(value = "type") @ApiParam("5--企业微信") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return companyInfoService.getBindInfo(WechatId,userId,type);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
}
